package com.ruoyi.web.controller.module.market;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.market.domain.Coupon;
import com.ruoyi.market.service.ICouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 卡券信息
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/market/coupon")
public class CouponController extends BaseController
{
    @Autowired
    private ICouponService couponService;

    /**
     * 获取卡券列表
     */
    @PostMapping("/couponList")
    public TableDataInfo list(@RequestBody Coupon coupon, HttpServletRequest request)
    {
        String shopId = request.getHeader("ShopId"); // 店铺id
        if(shopId != null && !shopId.equals("")){
            coupon.setShopId(shopId);
        }
        startPageByPost();
        List<Coupon> list = couponService.selectCouponList(coupon);
        return getDataTable(list);
    }

    @Log(title = "卡券管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("@ss.hasPermi('market:coupon:export')")
    @PostMapping("/couponExport")
    public void export(HttpServletResponse response, Coupon coupon)
    {
        List<Coupon> list = couponService.selectCouponList(coupon);
        ExcelUtil<Coupon> util = new ExcelUtil<Coupon>(Coupon.class);
        util.exportExcel(response, list, "卡券数据");
    }

    @Log(title = "卡券管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('market:coupon:import')")
    @PostMapping("/couponImportData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<Coupon> util = new ExcelUtil<Coupon>(Coupon.class);
        List<Coupon> couponList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = couponService.importCoupon(couponList, updateSupport, operName);
        return success(message);
    }

    @PostMapping("/couponImportTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<Coupon> util = new ExcelUtil<Coupon>(Coupon.class);
        util.importTemplateExcel(response, "卡券数据");
    }

    /**
     * 根据卡券编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('market:coupon:query')")
    @GetMapping(value = { "/", "/{couponId}" })
    public AjaxResult getInfo(@PathVariable(value = "couponId", required = false) Long couponId)
    {
        AjaxResult ajax = AjaxResult.success();
        if (StringUtils.isNotNull(couponId))
        {
            Coupon sysCoupon = couponService.selectCouponById(couponId);
            ajax.put(AjaxResult.DATA_TAG, sysCoupon);
        }
        return ajax;
    }

    /**
     * 新增卡券
     */
    @PreAuthorize("@ss.hasPermi('market:coupon:add')")
    @Log(title = "卡券管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Coupon coupon, HttpServletRequest request)
    {
        String shopId = request.getHeader("ShopId"); // 店铺id
        if(shopId != null && !shopId.equals("")){
            coupon.setShopId(shopId);
        }
        if (!couponService.checkCouponUnique(coupon))
        {
            return error("新增卡券'" + coupon.getCouponTitle() + "'失败，已存在");
        }
        coupon.setCreateBy(getUsername());
        return toAjax(couponService.insertCoupon(coupon));
    }

    /**
     * 修改卡券
     */
    @PreAuthorize("@ss.hasPermi('market:coupon:edit')")
    @Log(title = "卡券管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Coupon coupon)
    {
        if (!couponService.checkCouponUnique(coupon))
        {
            return error("修改卡券'" + coupon.getCouponTitle() + "'失败，已存在");
        }
        coupon.setUpdateBy(getUsername());
        return toAjax(couponService.updateCoupon(coupon));
    }

    /**
     * 删除卡券
     */
    @PreAuthorize("@ss.hasPermi('market:coupon:remove')")
    @Log(title = "卡券管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{couponIds}")
    public AjaxResult remove(@PathVariable Long[] couponIds)
    {
        return toAjax(couponService.deleteCouponByIds(couponIds));
    }

    /**
     * 状态修改
     */
    @PreAuthorize("@ss.hasPermi('market:coupon:edit')")
    @Log(title = "卡券管理", businessType = BusinessType.UPDATE)
    @PutMapping("/couponChangeStatus")
    public AjaxResult changeStatus(@RequestBody Coupon coupon)
    {
        coupon.setUpdateBy(getUsername());
        return toAjax(couponService.updateCouponStatus(coupon));
    }
}
